 /*///////////////////////////////////////////////////////////////////////////////
-- Author:      liuhx
-- Created:     Wed Nov 28 11:53:41 CST 2018
-- Description:
-- If you have any question,please email to liuhx@gentlesoft.com.cn
////////////////////////////////////////////////////////////////////////////////*/
package com.ctsi.question.service;

 import com.ctsi.commons.util.UtilValidate;
 import com.ctsi.commons.util.id.UUIDGenerator;
 import com.ctsi.commons.util.response.Page;
 import com.ctsi.framework.mybatis.support.MybatisGenericDao;
 import com.ctsi.framework.security.util.SecurityUtil;
 import com.ctsi.organise.common.api.PersonServiceProxy;
 import com.ctsi.question.entity.Expert;
 import com.ctsi.question.entity.HangyeLevel1;
 import com.ctsi.question.entity.ZhuanJiaList;
 import com.ctsi.security.ISecurity;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;

 import java.util.List;
 import java.util.Map;
 import java.util.Set;


 /**
 * Expert Service 类
 * @author   liuhx
 * @description  Expert
 * @created	 Wed Nov 28 11:53:41 CST 2018
 */

@Service
public class ExpertService {
	private static final Logger logger=LoggerFactory.getLogger(ExpertService.class);
	@Autowired
	private MybatisGenericDao dao;




	 /**
	  * 根据questionId查询专家列表（详情回显）
	  * @param id
	  * @return
	  */
	 public List<String> selectFullNameById(String id){
		 logger.debug("selectFullNameById",id);
		 return (List<String>) dao.queryForList(Expert.class.getName()+".selectFullNameById",id);
	 }
/*
* 查询该回答是否为专家
* */

	public List<Expert> selectIsZhuanJia(Expert expert){
		logger.debug("selectIsZhuanJia {}",expert);
		return (List<Expert>)dao.queryForList(Expert.class.getName()+".selectIsZhuanJia",expert);
	}

	/*
	* 查询该用户对应的专家列表
	*
	* */
	public List<ZhuanJiaList> selectZhuanJiaList(Map map){
	    logger.debug("selectZhuanJiaList {}",map);
	    return (List<ZhuanJiaList>) dao.queryForList(Expert.class.getName()+".selectZhuanJiaList",map);
    }
    /*
    * 查询一级行业名称
    * */
    public List<HangyeLevel1> selectDicTreeLevel1(){
        logger.debug("selectDicTreeLevel1 {}");
        return (List<HangyeLevel1>) dao.queryForList(Expert.class.getName()+".selectDicTreeLevel1");
    }
/*
* 是否采纳
* */
public  int updateIsCaiNa(Expert expert){
    logger.debug("updateAnswersById {}",expert);
    return dao.update(Expert.class.getName()+".updateAnswersById",expert);
}
	/**
	 * 添加
	 */
	public int insertExpert(Expert expert){
		if(UtilValidate.isEmpty(expert.getId()))
			expert.setId(UUIDGenerator.getId());
		logger.debug("insert {}",expert);
		return  dao.insert(expert);
	}
	/**
	 * 动态更新
	 */
	public int updateExpertByPKSelective(Expert expert){
		logger.debug("updateByPrimaryKeySelective {}",expert);
		return dao.update(Expert.class.getName()+".updateByPrimaryKeySelective", expert);
	}
	/**
	 * 更新
	 */
	public int updateExpert(Expert expert){
		logger.debug("update {}",expert);
		return dao.update(expert);
	}


	public Expert editExpertById(String id){
		if(UtilValidate.isEmpty(id))
			return null;
		logger.debug("editById {}",id);
		return (Expert)dao.queryForObject(Expert.class.getName()+".edit", id);
	}
	/**
	 * 根据ID查找 Expert对象
	 */
	public Expert findExpertById(String id){
		if(UtilValidate.isEmpty(id))
			return null;
		logger.debug("findById {}",id);
		return dao.get(Expert.class, id);
	}
	/**
	 * 删除Expert对象
	 */
	public int removeExpert(String id){
		if(UtilValidate.isNotEmpty(id))
			return dao.delete(Expert.class.getName()+".delete", id);
			//return dao.remove(id);
		return 0;
	}

	/**
	 * 详细页查询
	 */
	public Expert findByDetail(String id){
		return (Expert)dao.queryForObject(Expert.class.getName()+".findByDetail",id);
	}
	/**
	 * 多条件查询结果
	 */
	public Page<Expert> findByMulitCondition(Map<String,Object> param , int start , int pageSize){
		logger.debug("findByMulitCondition {}",param);
		if(pageSize<0||pageSize>2000)
			pageSize=Page.DEFAULT_PAGE_SIZE;
		if(start!=-1)
			return dao.queryPage(Expert.class.getName()+".selectMulitCondition", param, start, pageSize);
		else{
			throw new RuntimeException("参数不正确 !!");
		}
	}

	@Autowired
	private ISecurity iSecurity;
//
	public void test(String userloginId,Map<String,Object> params , int start , int pageSize){
	    // 先判断是否启用了控制权限
        if (!iSecurity.isControl(userloginId,"organChain","SELECT")) {
            // 直接查询

        }else {
            //控制权限
            Set<String> dataObjectIds = iSecurity.findDataObjectIds(userloginId,"organChain","SELECT");
            if (UtilValidate.isEmpty(dataObjectIds)) {
                // 返回空列表
                return ;
            }else {
                // 参数
                params.put("dataObjectIds",dataObjectIds);
                dao.queryPage(Expert.class.getName()+".xxxx",params,start,pageSize);
            }
        }

        personServiceProxy.getOrganise4User(SecurityUtil.getUser().getUserId());

	}

     @Autowired
     private PersonServiceProxy personServiceProxy;

}
